package logging

import (
	"fmt"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/duke-git/lancet/v2/datetime"
	"time"
)

// LogDataInput 创建一个带滚动条的容器
var LogDataInput = widget.NewLabel("")
var LogScrollContainer = container.NewScroll(LogDataInput)

// LogChan 日志打印
var LogChan = make(chan string)

func init() {
	go logInfoAdd(LogChan)
}

func logInfoAdd(logChan chan string) {
	for {
		select {
		case data := <-logChan:
			logData := fmt.Sprintf("%s - %s", datetime.GetNowDateTime(), data)
			fmt.Println(logData)
			LogDataInput.SetText(LogDataInput.Text + "\n" + logData)
			LogDataInput.Refresh()
			LogScrollContainer.ScrollToBottom()
		default:
			time.Sleep(time.Second * 1)
			break
		}
	}
}
